<template>
    <div>
      <h1>优惠卷修改</h1>
      <table class="table table-bordered">
        <tbody>
            <tr>
                <td>优惠卷类型</td>
                <td>
                    <select v-model="info.DisType">
                        <option :value="-1">请选择</option>
                        <option :value="1">代金券</option>
                        <option :value="2">折扣券</option>
                        <option :value="3">兑换券</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>优惠券标题</td>
                <td>
                   <input type="text" v-model="info.Tital">
                </td>
            </tr>
            <tr>
                <td>优惠券副标题</td>
                <td>
                   <input type="text" v-model="info.Ftital">
                </td>
            </tr>
            <tr>
                <td>优惠卷图片</td>
                <td>
                   <input type="file" @change="img">
                   <img :src="info.Photo" width="100" height="100" v-if="info.Photo!=''">
                </td>
            </tr>
            <tr>
                <td>库存</td>
                <td>
                   <input type="text" v-model="info.Numberd">
                </td>
            </tr>
            <tr>
                <td>限领个数</td>
                <td>
                   <input type="text"  v-model="info.PeopleCount" disabled>
                </td>
            </tr>
            <tr>
                <td>使用条件</td>
                <td>
                   <input type="text" v-model="info.PayMoney">
                </td>
            </tr>
            <tr v-if="info.DisType==1">
                <td>减免金额</td>
                <td>
                   <input type="text" v-model="info.SbuMoney">
                </td>
            </tr>
            <tr v-if="info.DisType==2">
                <td>折扣</td>
                <td>
                   <input type="text" v-model="info.ZheKou">
                </td>
            </tr>
            <tr>
                <td>有效期</td>
                <td>
                   <input type="date" v-model="info.BaginTime">~<input type="date" v-model="info.EndTime">
                </td>
            </tr>
            <tr>
                <td>适用业务</td>
                <td>
                   <input type="checkbox" name="1" v-model="info.service" value="订房">订房
                   <input type="checkbox" name="1" v-model="info.service" value="商城">商城
                   <input type="checkbox" name="1" v-model="info.service" value="食堂">食堂
                   <input type="checkbox" name="1" v-model="info.service" value="付款码">付款码
                </td>
            </tr>
            <tr>
                <td>使用须知</td>
                <td>
                   <textarea cols="50" rows="5" v-model="info.Notice"></textarea>
                </td>
            </tr>
            <tr>
                <td>适用门店</td>
                <td>
                   <select v-model="info.ShopName">
                    <option value="">请选择</option>
                    <option v-for="item in data.list" :value="item.ShopName" >{{item.ShopName}}</option>
                   </select>
                </td>
            </tr>
            <tr>
                <td>服务电话</td>
                <td>
                   <input type="text" maxlength="11" v-model="info.Phone">
                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td>
                   <input type="radio" name="state" v-model="info.State" :value="1">发布
                   <input type="radio" name="state" v-model="info.State" :value="0">不发布
                </td>
            </tr>
            <tr>
                <td>优惠卷颜色</td>
                <td>
                   <input type="color" v-model="info.color">
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                   <input type="button" value="更新" class="btn btn-success" @click="ok">
                </td>
            </tr>
        </tbody>
      </table>
    </div>
</template>

<script setup lang="ts">
//响应式数据
import {ref,reactive,onMounted} from 'vue';
import axios from 'axios';
import moment from 'moment';
//跳转
import {useRouter,useRoute} from 'vue-router';
const router=useRouter();
const route=useRoute();

let info:any=reactive({
  "DisType":'',
  "Tital": "",
  "Ftital": "",
  "Photo": "",
  "Numberd": '',
  "PeopleCount": 1,
  "PayMoney": '',
  "SbuMoney":0,
  "ZheKou":10,
  "BaginTime": "",
  "EndTime": "",
  "service":[],
  "Notice": "",
  "ShopName": "",
  "Phone": "",
  "Color": "",
  "State": 1,
})
//-----------------门店下拉框-----------------------
//向后端传递的查询参数
let info1:any=reactive({
    pageindex:1,
    pagesize:5,
    ShopName:'',
    Time1:'',
    Time2:'',
})
//声明变量接收数据
let data:any=reactive({
    list:[],
    totalCount:0,
    totalPage:0,
})
//生命周期函数
onMounted(()=>{
    getlist();
    detail();
})
//获取门店列表
const getlist=()=>{
    console.log(info);
    axios({
        url:'https://localhost:7193/api/Shop/GetShoplist',
        method:'get',
        params:info1,
    })
    .then(res=>{
        console.log(res);
        Object.assign(data,res.data);
    })
}
//-------------------------分页-----------------------------
const img=(e:any)=>{
    //获取你上传的图片
    let obj=e.target.files[0];
    //将上传的图片转成FormData格式
    let f =new FormData();
    f.append('file',obj);

    axios({
        url:'https://localhost:7193/api/Imgs/Imgs',
        method:'post',
        data:f,
    })
    .then((res)=>{
        console.log(res);
        if(res.data=='不是图片'){
            alert('上传的不是图片');
            return;
        }
        if(res.data=='图片大于2M'){
            alert('图片大于2M');
            return;
        }
        info.Photo=res.data;
    })
}
//-------------------反填-----------------
const detail=()=>{
    axios({
        url:'https://localhost:7193/api/DisCount/FtDis',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then(res=>{
        console.log(res);
        Object.assign(info,res.data);
        info.BaginTime=res.data.BaginTime.substring(0,10);
        info.EndTime=res.data.EndTime.substring(0,10);
        info.service=res.data.service.split(',');
    })
}
//-----------------------修改--------------
const ok=()=>{
    info.service=info.service.toString();
    axios({
        url:'https://localhost:7193/api/DisCount/UpdateDis',
        method:'put',
        data:info,
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            router.push('/discountlist')
        }
        else{
            alert('修改失败');
        }
    })
}
</script>

<style scoped>

</style>    